package com.dg.contorller;

import java.util.List;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dg.pojo.Person;
import com.dg.service.PersonService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/person")
public class PersonController {

    @Resource(name = "personService")
    private PersonService personService;

    //返回bootstrap-table格式数据（返回string格式和json一样）
    //只查person
    @RequestMapping("/getPersonData")
    @ResponseBody
    public JSONObject getPersonData(String pageNumber, String pageSize) {

        int Number = new Integer(pageNumber);
        int Size = new Integer(pageSize);

        PageHelper.startPage(Number, Size);
        List<Person> list = (List<Person>) personService.findAll();

        PageInfo<Person> pageInfo = new PageInfo<Person>(list);
        long total = pageInfo.getTotal(); //获取总记录数

        JSONObject json = new JSONObject();
        json.put("total", total);
        json.put("rows", list);

        System.out.println(json);
        return json;
    }

    //返回bootstrap-table格式数据（返回string格式和json一样）
    //关联dept查询
    @RequestMapping("/getPersonDataCom")
    @ResponseBody
    public JSONObject getPersonDataCom(String pageNumber, String pageSize) {


        JSONObject json = personService.findAllPD(pageNumber, pageSize);


        return json;
    }

    //add
    @RequestMapping("/addPerson")
    @ResponseBody
    public JSONObject addPerson(Person person) {

        JSONObject json = personService.addPerson(person);

        return json;
    }


}